﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PlanningGIS.Util.Data;
using System.IO;

namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// 数据库连接配置
    /// </summary>
    public partial class FrmConnect : Office2007Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmConnect()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 加载窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmConnect_Load(object sender, EventArgs e)
        {
            cmbDbTypes.Items.AddRange(new string[]{
                "Oracle","Access" 
            });
            cmbDbTypes.SelectedIndex = 0;
            UseType = zjgisUseType.Save;
        }

        /// <summary>
        /// 获取或设置数据库连接信息
        /// </summary>
        public DbConnectionInfo Connection { get; set; }

        /// <summary>
        /// 用途
        /// </summary>
        public zjgisUseType UseType { get; set; }

        /// <summary>
        /// 选择数据库类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbDbTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cmbDbTypes.Text)
            {
                case "Sql":
                case "Oracle":
                    tbctl.SelectedTab = tbitmDb;
                    break;
                case "Access":
                case "文件数据库":
                case "个人数据库":
                    tbctl.SelectedTab = tbitmLocal;
                    break;
            }
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!CheckValue()) return;
            if (Connection == null) Connection = new DbConnectionInfo();
            switch (cmbDbTypes.Text)
            {
                case "Sql":
                case "Oracle":
                    if (string.IsNullOrWhiteSpace(Connection.Name))
                        Connection.Name = txtServer.Text + "-" + txtUser.Text;
                    Connection.Server = txtServer.Text;
                    Connection.DBType = cmbDbTypes.Text;
                    Connection.UserID = txtUser.Text;
                    Connection.Password = txtPassword.Text;
                    Connection.SID = txtDb.Text;
                    break;
                case "Access":
                case "文件数据库":
                case "个人数据库":
                    if (string.IsNullOrWhiteSpace(Connection.Name))
                        Connection.Name = txtPath.Text;
                    Connection.Server = txtPath.Text;
                    break;
            }
            Connection.DBType = cmbDbTypes.Text;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        /// <summary>
        /// 检查输入的信息是否正确
        /// </summary>
        /// <returns></returns>
        private bool CheckValue()
        {
            if (tbctl.SelectedTab == tbitmDb)
            {
                if (string.IsNullOrWhiteSpace(txtServer.Text))
                {
                    txtServer.Focus();
                    return false;
                }
                if (string.IsNullOrWhiteSpace(txtDb.Text))
                {
                    txtDb.Focus();
                    return false;
                }
                if (string.IsNullOrWhiteSpace(txtUser.Text))
                {
                    txtUser.Focus();
                    return false;
                }
                if (string.IsNullOrWhiteSpace(txtPassword.Text))
                {
                    txtPassword.Focus();
                    return false;
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(txtPath.Text))
                {
                    txtPath.Focus();
                    return false;
                }
            }
            return true;
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (cmbDbTypes.Text.Equals("Access", StringComparison.CurrentCultureIgnoreCase))
            {
                if (UseType == zjgisUseType.Save)
                {
                    saveFileDialog.Filter = "Access数据库(*.mdb)|*.mdb";
                    if (saveFileDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                    {
                        txtPath.Text = saveFileDialog.FileName;
                    }
                }
                else
                {
                    openFileDialog.Filter = "Access数据库(*.mdb)|*.mdb";
                    if (openFileDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                    {
                        txtPath.Text = openFileDialog.FileName;
                    }
                }
                
            }
            else
            {
                SaveDataDialog dlg = new SaveDataDialog();
                if (dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    txtPath.Text = dlg.FullName;
                }
            }
            
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        public enum zjgisUseType
        {
            /// <summary>
            /// 打开
            /// </summary>
            Open,
            /// <summary>
            /// 保存
            /// </summary>
            Save
        }
    }
}
